﻿using System;
using KPIS.GERP.GAMS.MODEL;
using KPIS.DBM;
using System.Configuration;
using System.Data;
using System.Collections.Generic;


namespace KPIS.GERP.GAMS.DAL
{
    public class D_ACM_MAS_FISCAL_Period
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_ACM_MAS_FISCAL_Period> GetList()
        {
            DataRowCollection drc = null;
            IList<M_ACM_MAS_FISCAL_Period> infoList = new List<M_ACM_MAS_FISCAL_Period>();
            try
            {
                dbManager.Open();

                string strSQL = " SELECT fiscal_period,fiscal_period_code,fiscal_period_name,fiscal_period_remark,fiscal_start_date, fiscal_end_date "
                                + " FROM acm_mas_fiscal_period "
                                + " WHERE record_status not like 'D' "
                                + " ORDER BY fiscal_period_name ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ACM_MAS_FISCAL_Period info = new M_ACM_MAS_FISCAL_Period();
                        info.fiscal_period = Convert.ToInt32(drc[i]["fiscal_period"]);
                        info.fiscal_period_code = drc[i]["fiscal_period_code"].ToString();
                        info.fiscal_period_name = drc[i]["fiscal_period_name"].ToString();
                        info.fiscal_period_remark = drc[i]["fiscal_period_remark"].ToString();
                        info.fiscal_start_date = Convert.ToDateTime(drc[i]["fiscal_start_date"].ToString());
                        info.fiscal_end_date = Convert.ToDateTime(drc[i]["fiscal_end_date"].ToString());
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }

        public M_ACM_MAS_FISCAL_Period GetListFillBySeq(Nullable<int> fiscal_period)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ACM_MAS_FISCAL_Period info = new M_ACM_MAS_FISCAL_Period();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@fiscal_period", fiscal_period);

                strSQL = " SELECT fiscal_period,fiscal_period_code,fiscal_period_name,fiscal_period_remark,fiscal_start_date, fiscal_end_date "
                        + " FROM acm_mas_fiscal_period "
                        + " WHERE record_status not like 'D' "
                        + " AND fiscal_period = @fiscal_period "
                        + " ORDER BY fiscal_period DESC ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.fiscal_period = Convert.ToInt32(drc[0]["fiscal_period"]);
                    info.fiscal_period_code = drc[0]["fiscal_period_code"].ToString();
                    info.fiscal_period_name = drc[0]["fiscal_period_name"].ToString();
                    info.fiscal_period_remark = drc[0]["fiscal_period_remark"].ToString();
                    info.fiscal_start_date = Convert.ToDateTime(drc[0]["fiscal_start_date"]);
                    info.fiscal_end_date = Convert.ToDateTime(drc[0]["fiscal_end_date"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public M_ACM_MAS_FISCAL_Period GetListFilterByDate(string tran_date)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ACM_MAS_FISCAL_Period info = new M_ACM_MAS_FISCAL_Period();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@tran_date", tran_date);

                strSQL = " SELECT fiscal_period,fiscal_period_code,fiscal_period_name,fiscal_period_remark,fiscal_start_date, fiscal_end_date "
                        + " FROM acm_mas_fiscal_period "
                        + " WHERE record_status not like 'D'"
                        + " AND (fiscal_start_date <= '"+tran_date+"' AND  fiscal_end_date >= '"+tran_date+"') "
                       // + " AND (fiscal_start_date >= @tran_date AND fiscal_end_date <= '@tran_date') "
                       // + (tran_date != null ? " AND (fiscal_start_date >= @tran_date AND fiscal_end_date <= @tran_date ) " : "")
                        + " ORDER BY fiscal_period DESC ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.fiscal_period = Convert.ToInt32(drc[0]["fiscal_period"]);
                    //info.fiscal_period_code = drc[0]["fiscal_period_code"].ToString();
                    //info.fiscal_period_name = drc[0]["fiscal_period_name"].ToString();
                    //info.fiscal_period_remark = drc[0]["fiscal_period_remark"].ToString();
                    //info.fiscal_start_date = Convert.ToDateTime(drc[0]["fiscal_start_date"]);
                    //info.fiscal_end_date = Convert.ToDateTime(drc[0]["fiscal_end_date"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(M_ACM_MAS_FISCAL_Period info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);
                dbManager.AddParameters(0, "@fiscal_period_code", info.fiscal_period_code);
                dbManager.AddParameters(1, "@fiscal_period_name", info.fiscal_period_name);
                dbManager.AddParameters(2, "@fiscal_period_remark", info.fiscal_period_remark);
                dbManager.AddParameters(3, "@record_status", info.record_status);
                dbManager.AddParameters(4, "@created_by", info.created_by);
                dbManager.AddParameters(5, "@created_when", info.created_when);
                dbManager.AddParameters(6, "@fiscal_start_date", info.fiscal_start_date);
                dbManager.AddParameters(7, "@fiscal_end_date", info.fiscal_end_date);

                strSQL = "INSERT INTO acm_mas_fiscal_period "
                    + " (fiscal_period_code,fiscal_period_name "
                    + ", fiscal_period_remark,record_status,created_by,created_when "
                    + ", fiscal_start_date ,fiscal_end_date )"
                    + "  VALUES("
                    + "  @fiscal_period_code"
                    + ", @fiscal_period_name"
                    + ", @fiscal_period_remark"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when "
                    + ", @fiscal_start_date "
                    + ", @fiscal_end_date)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_ACM_MAS_FISCAL_Period info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@fiscal_period", info.fiscal_period);
                dbManager.AddParameters(1, "@fiscal_period_code", info.fiscal_period_code);
                dbManager.AddParameters(2, "@fiscal_period_name", info.fiscal_period_name);
                dbManager.AddParameters(3, "@fiscal_period_remark", info.fiscal_period_remark);
                dbManager.AddParameters(4, "@record_status", info.record_status);
                dbManager.AddParameters(5, "@updated_by", info.updated_by);
                dbManager.AddParameters(6, "@updated_when", info.updated_when);
                dbManager.AddParameters(7, "@fiscal_start_date", info.fiscal_start_date);
                dbManager.AddParameters(8, "@fiscal_end_date", info.fiscal_end_date);

                strSQL = "UPDATE acm_mas_fiscal_period"
                    + "  SET fiscal_period_code = @fiscal_period_code"
                    + ", fiscal_period_name = @fiscal_period_name"
                    + ", fiscal_period_remark = @fiscal_period_remark"
                    + ", record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + ", fiscal_start_date = @fiscal_start_date "
                    + ", fiscal_end_date = @fiscal_end_date "
                    + "  WHERE fiscal_period = @fiscal_period";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public DataTable Report_FISCAL_Period()
        {
            string strSQL;
            DataTable dtb = null;
            try
            {
                dbManager.Open();

                strSQL = " SELECT fiscal_period,fiscal_period_code,fiscal_period_name,fiscal_period_remark "
                       + " fiscal_start_date , fiscal_end_date "
                       + " FROM acm_mas_fiscal_period "
                       + " WHERE record_status not like 'D' "
                       + " ORDER BY fiscal_period  ";

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return dtb;
        }



    }
}
